package com.datatrans.config;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;

/**
 * 数据迁移配置类
 */
public class TransferConfig implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    // 数据库类型枚举
    public enum DatabaseType {
        MYSQL, POSTGRESQL, MSSQL, ORACLE
    }
    
    // 迁移模式枚举
    public enum TransferMode {
        DELETE_THEN_COPY, UPDATE, IGNORE
    }
    
    // 源数据库配置
    private String sourceUrl;
    private String sourceUsername;
    private String sourcePassword;
    private DatabaseType sourceDbType;
    
    // 目标数据库配置
    private String targetUrl;
    private String targetUsername;
    private String targetPassword;
    private DatabaseType targetDbType;
    
    // 表配置 - 支持单表和多表
    private String sourceTable;  // 为了向后兼容保留
    private String targetTable;  // 为了向后兼容保留
    private List<TableMapping> tableMappings = new ArrayList<>();
    
    // 迁移模式
    private TransferMode transferMode;
    
    // Getters and Setters
    public String getSourceUrl() {
        return sourceUrl;
    }
    
    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }
    
    public String getSourceUsername() {
        return sourceUsername;
    }
    
    public void setSourceUsername(String sourceUsername) {
        this.sourceUsername = sourceUsername;
    }
    
    public String getSourcePassword() {
        return sourcePassword;
    }
    
    public void setSourcePassword(String sourcePassword) {
        this.sourcePassword = sourcePassword;
    }
    
    public DatabaseType getSourceDbType() {
        return sourceDbType;
    }
    
    public void setSourceDbType(DatabaseType sourceDbType) {
        this.sourceDbType = sourceDbType;
    }
    
    public String getTargetUrl() {
        return targetUrl;
    }
    
    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }
    
    public String getTargetUsername() {
        return targetUsername;
    }
    
    public void setTargetUsername(String targetUsername) {
        this.targetUsername = targetUsername;
    }
    
    public String getTargetPassword() {
        return targetPassword;
    }
    
    public void setTargetPassword(String targetPassword) {
        this.targetPassword = targetPassword;
    }
    
    public DatabaseType getTargetDbType() {
        return targetDbType;
    }
    
    public void setTargetDbType(DatabaseType targetDbType) {
        this.targetDbType = targetDbType;
    }
    
    // 为了向后兼容保留的单表方法
    public String getSourceTable() {
        if (sourceTable != null) {
            return sourceTable;
        }
        // 如果有表映射，返回第一个源表
        if (!tableMappings.isEmpty()) {
            return tableMappings.get(0).getSourceTable();
        }
        return null;
    }
    
    public void setSourceTable(String sourceTable) {
        this.sourceTable = sourceTable;
        // 如果还没有表映射，创建一个默认的映射
        if (this.tableMappings.isEmpty()) {
            TableMapping mapping = new TableMapping();
            mapping.setSourceTable(sourceTable);
            mapping.setTargetTable(sourceTable); // 默认目标表等于源表
            this.tableMappings.add(mapping);
        } else {
            // 更新第一个映射的源表
            this.tableMappings.get(0).setSourceTable(sourceTable);
        }
    }
    
    public String getTargetTable() {
        if (targetTable != null) {
            return targetTable;
        }
        // 如果有表映射，返回第一个目标表
        if (!tableMappings.isEmpty()) {
            return tableMappings.get(0).getTargetTable();
        }
        return null;
    }
    
    public void setTargetTable(String targetTable) {
        this.targetTable = targetTable;
        // 如果还没有表映射，创建一个默认的映射
        if (this.tableMappings.isEmpty()) {
            TableMapping mapping = new TableMapping();
            mapping.setSourceTable(targetTable); // 默认源表等于目标表
            mapping.setTargetTable(targetTable);
            this.tableMappings.add(mapping);
        } else {
            // 更新第一个映射的目标表
            this.tableMappings.get(0).setTargetTable(targetTable);
        }
    }
    
    // 多表映射相关方法
    public List<TableMapping> getTableMappings() {
        return tableMappings;
    }
    
    public void setTableMappings(List<TableMapping> tableMappings) {
        this.tableMappings = tableMappings;
    }
    
    public void addTableMapping(String sourceTable, String targetTable) {
        TableMapping mapping = new TableMapping();
        mapping.setSourceTable(sourceTable);
        mapping.setTargetTable(targetTable != null ? targetTable : sourceTable); // 默认目标表等于源表
        this.tableMappings.add(mapping);
    }
    
    public void addTableMapping(String sourceTable) {
        addTableMapping(sourceTable, null); // 目标表默认等于源表
    }
    
    public TransferMode getTransferMode() {
        return transferMode;
    }
    
    public void setTransferMode(TransferMode transferMode) {
        this.transferMode = transferMode;
    }
    
    /**
     * 表映射内部类
     */
    public static class TableMapping implements Serializable {
        private static final long serialVersionUID = 1L;
        
        private String sourceTable;
        private String targetTable;
        
        public String getSourceTable() {
            return sourceTable;
        }
        
        public void setSourceTable(String sourceTable) {
            this.sourceTable = sourceTable;
        }
        
        public String getTargetTable() {
            return targetTable != null ? targetTable : sourceTable; // 默认目标表等于源表
        }
        
        public void setTargetTable(String targetTable) {
            this.targetTable = targetTable;
        }
    }
}